  var userInfo = {
    height: null,
    sex: null,
    age: null
  }
  var bodyFatData = {
    weight: '-.-',             //体重*
    bmi: '-.-',                //
    fat_rate: '-.-',           //体脂率*
    muscle_rate: '-.-',        //肌肉率*
    water_content: '-.-',      //水分*
    bone_mass: '-.-',          //骨量*
    basal_metabolism: '-.-',   //基础代谢*
    visceral_fat: '-.-',       //内脏脂肪*
    subcutaneous_fat: '-.-',   //皮下脂肪*
    protein_rate: '-.-',       //蛋白率*
    standard_weight: '-.-',    //标准体重
    weight_control: '-.-',     //体重控制量
    except_fat_weight: '-.-',  //去脂体重  
    muscle_volume: '-.-',      //肌肉量
    fat_volume: '-.-',         //脂肪量
    body_age: '-.-',           //身体年龄
    protein_content: '-.-'     //蛋白量
  }
  var bodyFatDataEvaluate = {
    weight: '-.-',             //体重*
    bmi: '-.-',                //
    fat_rate: '-.-',           //体脂率*
    muscle_rate: '-.-',        //肌肉率*
    water_content: '-.-',      //水分*
    bone_mass: '-.-',          //骨量*
    basal_metabolism: '-.-',   //基础代谢*
    visceral_fat: '-.-',       //内脏脂肪*
    subcutaneous_fat: '',   //皮下脂肪*
    protein_rate: '-.-',       //蛋白率*
    standard_weight: ' ',    //标准体重
    weight_control: '',     //体重控制量
    except_fat_weight: '',  //去脂体重  
    muscle_volume: '-.-',      //肌肉量
    fat_volume: '-.-',         //脂肪量
    body_age: '',           //身体年龄
    protein_content: '-.-'     //蛋白量
  }

  function getBodyFatDataArray(){
    return [
      { name: '脂肪量', value: bodyFatData.fat_volume, evaluate: bodyFatDataEvaluate.fat_volume, unit: 'kg' },
      // { name: '基础代谢', value: bodyFatData.basal_metabolism, evaluate: bodyFatDataEvaluate.basal_metabolism, unit: 'kcal' },
      { name: '蛋白量', value: bodyFatData.protein_content, evaluate: bodyFatDataEvaluate.protein_content, unit: 'kg' },
      { name: '内脏脂肪', value: bodyFatData.visceral_fat, evaluate: bodyFatDataEvaluate.visceral_fat, unit: '' },
      // { name: '肌肉率', value: bodyFatData.muscle_rate, evaluate: bodyFatDataEvaluate.muscle_rate, unit: '%' },
      // { name: '骨量', value: bodyFatData.bone_mass, evaluate: bodyFatDataEvaluate.bone_mass, unit: 'kg' },
      { name: '蛋白率', value: bodyFatData.protein_rate, evaluate: bodyFatDataEvaluate.protein_rate, unit: '%' },
      // { name: '水分', value: bodyFatData.water_content, evaluate: bodyFatDataEvaluate.water_content, unit: '' },
      // { name: '肌肉量', value: bodyFatData.muscle_volume, evaluate: bodyFatDataEvaluate.muscle_volume, unit: 'kg' },
      { name: '标准体重', value: bodyFatData.standard_weight, evaluate: bodyFatDataEvaluate.standard_weight, unit: 'kg' },
      { name: '体重控制量', value: bodyFatData.weight_control, evaluate: bodyFatDataEvaluate.weight_control, unit: 'kg' },
      { name: '皮下脂肪', value: bodyFatData.subcutaneous_fat, evaluate: bodyFatDataEvaluate.subcutaneous_fat, unit: '%' },
      // { name: '去脂体重', value: bodyFatData.except_fat_weight, evaluate: bodyFatDataEvaluate.except_fat_weight, unit: 'kg' },
      { name: '肥胖等级', value: bodyFatDataEvaluate.bmi, evaluate: '', unit: '' },
      { name: '身体年龄', value: bodyFatData.body_age, evaluate: bodyFatDataEvaluate.evaluate, unit: '' },
      // { name: '-.-', value: '-.-', evaluate: '-.-', unit: '' },
      // { name: '-.-', value: '-.-', evaluate: '-.-', unit: '' },
    ]
  }
  function getAllBodyFatDataArray(){
    let bodyFatDataArray = [
      { name: '体重', value: bodyFatData.weight, evaluate: bodyFatDataEvaluate.weight, unit: 'kg' },
      { name: 'BMI', value: bodyFatData.bmi, evaluate: bodyFatDataEvaluate.bmi, unit: '' },
      { name: '体脂率', value: bodyFatData.fat_rate, evaluate: bodyFatDataEvaluate.fat_rate, unit: '%' },
    ]
    return bodyFatDataArray.concat(getBodyFatDataArray());
  }
  // function getFatValue(value){
  //   return value === null ? '-.-' : value
  // }
  //设置BMI值
  function setBmi(value){
    let title = '-.-';
    if (value < 18.5) {
      title = '偏低'
    } else if (value < 2.4) {
      title = '标准'
    } else if (value < 2.8) {
      title = '偏高'
    } else {
      title = '高'
    }
    bodyFatDataEvaluate.bmi = title
    bodyFatDataEvaluate.weight = title
    return title
  }

  //设置基础代谢
  function setBasalMetabolism(value) {
    let title = '-.-';
    if(userInfo.sex == 1){
      if (userInfo.age <= 2) {
        if(60 * bodyFatData.weight - 54 < bodyFatData.basal_metabolism){
          title = '达标'
        }else{
          title = '未达标'
        }
      } else if (userInfo.age <= 9) {
        if (22.7 * bodyFatData.weight + 495 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else if (userInfo.age <= 17) {
        if (17.5 * bodyFatData.weight + 651 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else if (userInfo.age <= 29) {
        if (15.3 * bodyFatData.weight + 679 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else {
        if (11.6 * bodyFatData.weight + 879 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      }

    }else{
      if (userInfo.age <= 2) {
        if (61 * bodyFatData.weight - 51 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else if (userInfo.age <= 9) {
        if (22.5 * bodyFatData.weight + 499 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else if (userInfo.age <= 17) {
        if (12.2 * bodyFatData.weight + 746 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else if (userInfo.age <= 29) {
        if (14.7 * bodyFatData.weight + 496 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      } else {
        if (8.7 * bodyFatData.weight + 820 < bodyFatData.basal_metabolism) {
          title = '达标'
        } else {
          title = '未达标'
        }
      }
    }
    bodyFatDataEvaluate.basal_metabolism = title
  }


  //设置体脂率
  function setFatRate(value) {
    let title = '-.-';
    if (userInfo.sex == 1) {
      if (userInfo.age < 30) {
        if (value < 10) {
          title = '偏低'
        } else if (value < 21) {
          title = '标准'
        } else if (value < 26) {
          title = '偏高'
        } else {
          title = '高'
        }
      } else {
        if (value < 11) {
          title = '偏低'
        } else if (value < 22) {
          title = '标准'
        } else if (value < 27) {
          title = '偏高'
        } else {
          title = '高'
        }
      }
    } else {
      if (userInfo.age < 30) {
        if (value < 16) {
          title = '偏低'
        } else if (value < 24) {
          title = '标准'
        } else if (value < 30) {
          title = '偏高'
        } else {
          title = '高'
        }
      } else {
        if (value < 16) {
          title = '偏低'
        } else if (value < 27) {
          title = '标准'
        } else if (value < 30) {
          title = '偏高'
        } else {
          title = '高'
        }
      }
    }
    bodyFatDataEvaluate.fat_rate = title
    bodyFatDataEvaluate.fat_volume = title
    return title
  }
  //设置肌肉率
  function setMuscleRate(value) {
    let title = '-.-';
    if (userInfo.sex == 1) {
      if (value < 40) {
        title = '偏低'
      } else if (value < 60) {
        title = '标准'
      } else {
        title = '偏高'
      }
    } else {
      if (value < 30) {
        title = '偏低'
      } else if (value < 50) {
        title = '标准'
      } else {
        title = '偏高'
      }
    }
    bodyFatDataEvaluate.muscle_rate = title
    bodyFatDataEvaluate.muscle_volume = title
  }
  //设置水分
  function setWaterContent(value) {
    let title = '-.-';
    if (userInfo.sex == 1) {
      if (value < 55) {
        title = '偏低'
      } else if (value < 65) {
        title = '标准'
      } else {
        title = '偏高'
      }
    } else {
      if (value < 45) {
        title = '偏低'
      } else if (value < 60) {
        title = '标准'
      } else {
        title = '偏高'
      }
    }
    bodyFatDataEvaluate.water_content = title
  }
  //设置骨重
  function setBoneMass(value) {
    let title = '-.-';
    if (userInfo.sex == 1) {
      if (bodyFatData.weight< 60){
        if (value < 2.4) {
          title = '偏低'
        } else if (value < 2.6) {
          title = '标准'
        } else {
          title = '偏高'
        }
      } else if (bodyFatData.weight < 75){
        if (value < 28) {
          title = '偏低'
        } else if (value < 30) {
          title = '标准'
        } else {
          title = '偏高'
        }
      }else{
        if (value < 31) {
          title = '偏低'
        } else if (value < 33) {
          title = '标准'
        } else {
          title = '偏高'
        }
      }
    } else {
      if (bodyFatData.weight < 45) {
        if (value < 17) {
          title = '偏低'
        } else if (value < 19) {
          title = '标准'
        } else {
          title = '偏高'
        }
      } else if (bodyFatData.weight < 60) {
        if (value < 2.1) {
          title = '偏低'
        } else if (value < 2.3) {
          title = '标准'
        } else {
          title = '偏高'
        }
      } else {
        if (value < 2.4) {
          title = '偏低'
        } else if (value < 2.6) {
          title = '标准'
        } else {
          title = '偏高'
        }
      }
    }
    bodyFatDataEvaluate.bone_mass = title
  }

  //设置内脏脂肪
  function setVisceralFat(value) {
    let title = '-.-';
    if (value < 9) {
      title = '标准'
    } else if (value < 14) {
      title = '偏高'
    } else {
      title = '高'
    }
    bodyFatDataEvaluate.visceral_fat = title
  }

  //设置蛋白率
  function setProteinRate(value) {
    let title = '-.-';
    if(userInfo.sex == 1){
      if (value < 16) {
        title = '偏低'
      } else if (value < 18) {
        title = '标准'
      } else {
        title = '偏高'
      }
    }else{
      if (value < 14) {
        title = '偏低'
      } else if (value < 16) {
        title = '标准'
      } else {
        title = '偏高'
      }
    }
    bodyFatDataEvaluate.protein_rate = title
    bodyFatDataEvaluate.protein_content = title
  }
  //更新用户数据
  function setUserInfo(user){
    if (!(user.height && user.sex && user.birthday != null)){
      return false
    }
    userInfo.height = user.height
    userInfo.sex = user.sex
    userInfo.age = getAge(user.birthday)
  }
  //根据生日计算年龄
  function getAge(strBirthday) {
    let returnAge;
    let strBirthdayArr = strBirthday.split("-");
    let birthYear = strBirthdayArr[0];
    let birthMonth = strBirthdayArr[1];
    let birthDay = strBirthdayArr[2];

    let d = new Date();
    let nowYear = d.getFullYear();
    let nowMonth = d.getMonth() + 1;
    let nowDay = d.getDate();

    if (nowYear == birthYear) {
      returnAge = 0;//同年 则为0岁  
    }
    else {
      let ageDiff = nowYear - birthYear; //年之差  
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          let dayDiff = nowDay - birthDay;//日之差  
          if (dayDiff < 0) {
            returnAge = ageDiff - 1;
          }
          else {
            returnAge = ageDiff;
          }
        }
        else {
          let monthDiff = nowMonth - birthMonth;//月之差  
          if (monthDiff < 0) {
            returnAge = ageDiff - 1;
          }
          else {
            returnAge = ageDiff;
          }
        }
      }
      else {
        returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天  
      }
    }

    return returnAge;//返回周岁年龄  

  }  
module.exports = {
  userInfo: userInfo,
  setUserInfo: setUserInfo,
  bodyFatData: bodyFatData,
  bodyFatDataEvaluate: bodyFatDataEvaluate,
  getBodyFatDataArray: getBodyFatDataArray,
  setBmi: setBmi,
  setBasalMetabolism: setBasalMetabolism,
  setFatRate: setFatRate,
  setMuscleRate: setMuscleRate,
  setWaterContent: setWaterContent,
  setBoneMass: setBoneMass,
  setVisceralFat: setVisceralFat,
  setProteinRate: setProteinRate,
  getAllBodyFatDataArray: getAllBodyFatDataArray
}

